/*-------------------<-- Start of Description-->---------------------\ | Update the contents of an bookmark with either a variable from a | | data set; | | NOTE: the function will create a global variable __lastpath, such | | that you don't need to specify the path of the document again| | after you have used the function with a valid path at least | | once if you want to update bookmarks in the same document; | |---------------------<-- End of Description-->----------------------| |--------------------------------------------------------------------| |------------<-- Start of Files or Arguments Needed-->---------------| | Argument: | | bookmark: the name of the bookmark you want to update; | | data : the name of the dataset; | | var : variable name; | | format : the format you want to use for the variable to update | | the document; | | path : the path of the document you want to update, it will | | create a global macro variable named "__lastpath", | | which saves the path of the most recent used document.| | where : if the dataset has more than one observations, you | | can use this where statement to tell which observation| | you want to use to update the bookmark; | |------------<-- Start of Files or Arguments Needed-->---------------| |--------------------------------------------------------------------| |------------------<-- Start of Files Created-->---------------------| | Example: %bmark('b1',data1,x,5.0,path='c:\temp.doc'); | | Usage: %bmark(bookmark,data,var,format=,path=,wordref=,where=); | \-------------------<-- End of Files Created-->---------------------*/ %macro bmark(bookmark, data, var, format=, path=&__lastpath, where=); /*--------------------------------------------\ | Copy Right: Duo Zhou; | | Created: 2-27-2002 11:33pm; | | Modified: 6-5-2003 7:30pm; | | Purpose: update bookmarks in word document;| \--------------------------------------------*/ %global __lastpath; %local bookmark data var format path where; %if (%quote(&path) ne) %then %do; %let __lastpath=%trim(%left(%sysfunc(dequote(&path)))); %end; %else %if (%quote(&path) eq) and (%quote(&__lastpath) eq) %then %do; %put ==> Alert! You must provide a valid file path in order to update a bookmark.; %goto finish; %end; %let bookmark=%trim(%left(%sysfunc(dequote(&bookmark)))); %let __varnum=0; %let __vartype=; %let __dsid=0; %let __varnumstr=0; %if (%quote(&data) ne) %then %do; %if (%sysfunc(exist(&data))) %then %do; %let __dsid=%sysfunc(open(&data)); %let __varnumstr=%sysfunc(VARNUM(&__dsid,__str)); %let __varnum=%sysfunc(VARNUM(&__dsid,&var)); %let __vartype=%sysfunc(VARTYPE(&__dsid, &__varnum)); %let __rc=%sysfunc(close(&__dsid)); %end; %end; %if (&__varnum) or (%index(%nrbquote(&var), %nrbquote(%sysfunc(dequote(%quote(&var))))) > 1) %then %do; %if (%sysfunc(fileexist(&__lastpath))) %then %do; Filename __path dde "Winword|%trim(%left(%sysfunc(dequote(&__lastpath))))!&bookmark" notab lrecl=1048576; data _null_; file __path; %if (%quote(&format) ne) and (%quote(%upcase(&__vartype)) eq %quote(C)) %then %do; format &var &format; %end; %if (%sysfunc(exist(&data))) %then %do; set &data%if (%quote(&where) ne) or (&__varnumstr) %then %do; (%if (%quote(&where) ne) %then where=(&where); %if (&__varnumstr) %then drop=__str;) %end;; %end; %if (%quote(%upcase(&__vartype)) eq %quote(N)) and (%quote(&format) ne) %then %do; __str=trimn(left(put(&var, &format))); %end; %else %if (%quote(%upcase(&__vartype)) eq %quote(N)) and (%quote(&format) eq) %then %do; __str=trimn(left(put(&var, best12.))); %end; %else %if (%quote(%upcase(&__vartype)) eq %quote(C)) %then %do; __str=trimn(left(&var)); %end; %else %if (%index(%nrbquote(&var), %nrbquote(%sysfunc(dequote(%quote(&var))))) > 1) %then %do; __str=trim(left(&var)); %end; put __str; run; %end; %else %do; %put ==> Alert! File path is not valid.; %goto finish; %end; %end; %else %put ==> Alert! Dataset &data does not have variable named "&var"!; %finish: %mend bmark;